package cn.otrue.patienthealthmanager.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.provider.Settings;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import cn.otrue.patienthealthmanager.R;

/**
 * 对话框弹出的工具类，可弹出默认样式和自定义View的对话框
 * 
 * @author wujiankun
 * 
 */
public class DialogUtil {

	/**
	 * , 定义一个显示消息的对话框
	 * 
	 * @param ctx
	 * @param msg
	 * @param closeSelf
	 *            对话框关闭后是否也关闭当前的Activity
	 */
	public static void showDialog(final Context ctx, String msg,
			boolean closeSelf) {
		// 创建一个AlertDialog.Builder对象
		AlertDialog.Builder builder = new AlertDialog.Builder(ctx).setMessage(
				msg).setCancelable(false);
		if (closeSelf) {
			builder.setPositiveButton("确定", new OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// 结束当前Activity
					((Activity) ctx).finish();
				}
			});
		} else {
			// 如果无网络连接，刚弹出对话框，询问是否打开网络设置
			builder.setPositiveButton("确定", new OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					ctx.startActivity(new Intent(Settings.ACTION_SETTINGS));
				}
			}).setNegativeButton("取消", new OnClickListener() {
				// 如果用户不打算开启网络 ，就关闭当前的Activity
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// 结束当前Activity
					// ((Activity) ctx).finish();
				}
			});
		}
		builder.create().show();
	}

	/**
	 * 定义一个显示指定组件的对话框
	 * 
	 * @param ctx
	 * @param view
	 * @param cancelable
	 *            是否可忽略
	 */
	@SuppressWarnings("deprecation")
	public static AlertDialog showDialog(Context ctx, View view,
			boolean cancelable) {
		AlertDialog dlg = new AlertDialog.Builder(ctx)
				.setCancelable(cancelable).create();
		dlg.show();
		dlg.getWindow().setContentView(view);
		WindowManager.LayoutParams params = dlg.getWindow().getAttributes();
		WindowManager wm = (WindowManager) ctx
				.getSystemService(Context.WINDOW_SERVICE);
		params.width = wm.getDefaultDisplay().getWidth() * 5 / 6;
		params.height = WindowManager.LayoutParams.WRAP_CONTENT;
		dlg.getWindow().setAttributes(params);
		// 下面这一行弹出对话框时需要点击输入框才能弹出软键盘
		dlg.getWindow().clearFlags(
				WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
		// 加上下面这一行弹出对话框时软键盘随之弹出
		// dlg.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
		return dlg;
	}

	/**
	 * 显示一般的等待进度框(主线程中调用),返回被赋值的progressDialog,以方便在同一个类中关闭此ProgressDialog
	 * 
	 * @param context
	 * @param title
	 *            进度框标题
	 * @param message
	 *            进度框显示信息
	 * @param progressDialog
	 *            传入的全局变量，可为null
	 */
	public static ProgressDialog startProgressDialog(Context context,
			String title, String message, ProgressDialog progressDialog) {
		if (progressDialog == null) {
			progressDialog = new ProgressDialog(context);
		}
		progressDialog.setCancelable(false);
		// progressDialog.setTitle(title);
		progressDialog.setCanceledOnTouchOutside(false);
		progressDialog.setIcon(android.R.drawable.ic_dialog_info);
		progressDialog.setMessage(message);
		progressDialog.show();
		return progressDialog;
	}

	/**
	 * 显示自定义加载弹出框，图片增加旋转动画
	 * 
	 * @param context
	 * @param layoutId
	 * @param progressDialog
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static AlertDialog startProgressDialog(Context context,
			String message) {
		LayoutInflater inflater = LayoutInflater.from(context);
		View view = inflater.inflate(R.layout.progress_dialog, null);
		RelativeLayout rl = (RelativeLayout) view
				.findViewById(R.id.rl_catt_main);
		rl.getBackground().setAlpha(128);
		TextView messageText = (TextView) view
				.findViewById(R.id.tv_catt_dialogmsg);
		if (message != null && !"".equals(message)) {
			messageText.setText(message);
		}
		ImageView imageView = (ImageView) view
				.findViewById(R.id.gf_catt_gifview);
		Animation rotateAnimation = new RotateAnimation(0f, 359f,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				0.5f);
		LinearInterpolator lin = new LinearInterpolator();
		rotateAnimation.setInterpolator(lin);// 设置匀速旋转
		rotateAnimation.setDuration(1200); // 设置动画时间
		rotateAnimation.setRepeatCount(-1);// 设置为不停的旋转
		imageView.setAnimation(rotateAnimation);
		AlertDialog dlg = new AlertDialog.Builder(context).setCancelable(false)
				.create();
		dlg.show();
		dlg.getWindow().setContentView(view);
		WindowManager.LayoutParams params = dlg.getWindow().getAttributes();
		WindowManager wm = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		params.width = wm.getDefaultDisplay().getWidth() * 4 / 5;
		params.height = WindowManager.LayoutParams.WRAP_CONTENT;
		dlg.getWindow().setAttributes(params);
		rotateAnimation.startNow();
		return dlg;

	}

	/**
	 * 结束加载弹框
	 * 
	 * @param alertDialog
	 */
	public static void endProgressDialog(AlertDialog alertDialog) {
		if (alertDialog != null) {
			ImageView view = (ImageView) alertDialog
					.findViewById(R.id.gf_catt_gifview);
			if (view != null) {
				view.clearAnimation();
			}
			alertDialog.dismiss();
		}
	}

	/**
	 * 结束一般的进度进度框
	 * 
	 * @param progressDialog
	 *            要结束的对象
	 */
	public static void endProgressDialog(ProgressDialog progressDialog) {
		if (progressDialog != null) {
			progressDialog.dismiss();
			progressDialog = null;
		}
	}

	/**
	 * 弹出无网络对话框
	 * 
	 * @param context
	 * @param layoutId
	 */
	@SuppressWarnings("deprecation")
	public static AlertDialog showNetConnectionDialog(Context context,
			final OnCallBackListener onCallBackListener) {
		LayoutInflater inflater = LayoutInflater.from(context);
		int layoutId;

		layoutId = R.layout.dialog_net_connect;

		View view = inflater.inflate(layoutId, null);
		AlertDialog dlg = new AlertDialog.Builder(context).setCancelable(false)
				.create();
		dlg.show();
		dlg.getWindow().setContentView(view);
		WindowManager.LayoutParams params = dlg.getWindow().getAttributes();
		WindowManager wm = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		params.width = wm.getDefaultDisplay().getWidth() * 5 / 6;
		params.height = WindowManager.LayoutParams.WRAP_CONTENT;
		dlg.getWindow().setAttributes(params);
		// 下面这一行弹出对话框时需要点击输入框才能弹出软键盘
		dlg.getWindow().clearFlags(
				WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
		TextView leftButton = (TextView) view
				.findViewById(R.id.tv_catt_dialog_left);
		TextView rightButton = (TextView) view
				.findViewById(R.id.tv_catt_dialog_right);
		leftButton.setVisibility(View.GONE);
		leftButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (CommonUtil.isFastDoubleClick()) {
					return;
				}
				onCallBackListener.clickLeftButton();
			}
		});
		rightButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				onCallBackListener.clickRightButton();
			}
		});
		return dlg;
	}

	/**
	 * 弹出下载对话框
	 * 
	 * @param context
	 * @param layoutId
	 */
	@SuppressWarnings("deprecation")
	public static AlertDialog showDownloadDialog(Context context,
			int version_id, final OnCallBackListener onCallBackListener) {
		LayoutInflater inflater = LayoutInflater.from(context);
		int layoutId = R.layout.dialog_net_connect;

		View view = inflater.inflate(layoutId, null);
		AlertDialog dlg = new AlertDialog.Builder(context).setCancelable(false)
				.create();
		dlg.show();
		dlg.getWindow().setContentView(view);
		WindowManager.LayoutParams params = dlg.getWindow().getAttributes();
		WindowManager wm = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		params.width = wm.getDefaultDisplay().getWidth() * 5 / 6;
		params.height = WindowManager.LayoutParams.WRAP_CONTENT;
		dlg.getWindow().setAttributes(params);
		// 下面这一行弹出对话框时需要点击输入框才能弹出软键盘
		dlg.getWindow().clearFlags(
				WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
		TextView leftButton = (TextView) view
				.findViewById(R.id.tv_catt_dialog_left);
		TextView rightButton = (TextView) view
				.findViewById(R.id.tv_catt_dialog_right);
		TextView tipTextView = (TextView) view.findViewById(R.id.tv_tips_info);
		tipTextView.setText("亲，有新版本哦，是否立即下载？");
		leftButton.setText("立即下载");
		rightButton.setText("取消");
		leftButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (CommonUtil.isFastDoubleClick()) {
					return;
				}
				onCallBackListener.clickLeftButton();
			}
		});
		rightButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				onCallBackListener.clickRightButton();
			}
		});
		return dlg;
	}

	public static AlertDialog reloginDialog;

	/**
	 * 弹出重新登录对话框
	 * 
	 * @param context
	 * @param layoutId
	 */
	@SuppressWarnings("deprecation")
	public static AlertDialog showReloginDialog(Context context,
			String leftBtnText, String contentText,
			final OnCallBackListener onCallBackListener) {
		LayoutInflater inflater = LayoutInflater.from(context);
		SharedPreferences sharedPref = context.getSharedPreferences(
				MyConst.CONFIG_SP_NAME, Context.MODE_PRIVATE);
		// 获取版本类型
		int version_id = sharedPref.getInt(MyConst.VERSION_ID, -1);
		int layoutId = R.layout.dialog_net_connect;

		View view = inflater.inflate(layoutId, null);
		reloginDialog = new AlertDialog.Builder(context).setCancelable(false)
				.create();
		reloginDialog.show();
		reloginDialog.getWindow().setContentView(view);
		WindowManager.LayoutParams params = reloginDialog.getWindow()
				.getAttributes();
		WindowManager wm = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		params.width = wm.getDefaultDisplay().getWidth() * 5 / 6;
		params.height = WindowManager.LayoutParams.WRAP_CONTENT;
		reloginDialog.getWindow().setAttributes(params);
		// 下面这一行弹出对话框时需要点击输入框才能弹出软键盘
		reloginDialog.getWindow().clearFlags(
				WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
		TextView leftButton = (TextView) view
				.findViewById(R.id.tv_catt_dialog_left);
		view.findViewById(R.id.tv_catt_dialog_right).setVisibility(View.GONE);
		TextView tipTextView = (TextView) view.findViewById(R.id.tv_tips_info);
		tipTextView.setText(contentText);
		leftButton.setText(leftBtnText);
		leftButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				onCallBackListener.clickLeftButton();
			}
		});
		return reloginDialog;
	}

}
